package com.crb.crm.manage.service.biz.impl;

import com.crb.crm.manage.utils.DateUtil;
import com.crb.crm.manage.dao.biz.RestLogInfoMapper;
import com.crb.crm.manage.dao.biz.RestLogMapper;
import com.crb.crm.manage.entity.biz.RestLogDTO;
import com.crb.crm.manage.entity.biz.RestLogSearchDTO;
import com.crb.crm.manage.excel.model.RestLogModel;
import com.crb.crm.manage.excel.model.UserDeviceModel;
import com.crb.crm.manage.pojo.biz.RestLogWithBLOBs;
import com.crb.crm.manage.service.biz.RestLogService;
import com.crb.crm.manage.utils.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Package: com.crb.crm.manage.service.biz
 * Description：
 * Author: huangjian
 * Date:  2020/9/3 11:25
 * Modified By:
 */
@Service
public class RestLogServiceImpl implements RestLogService {
    private static final Logger logger = LoggerFactory.getLogger(RestLogServiceImpl.class);
    @Autowired
    private RestLogMapper mapper;
    @Autowired
    private RestLogInfoMapper infoMapper;

    @Override
    public PageDataResult getRestLogs(RestLogSearchDTO searchDTO, int page, int limit) {
        handleParam(searchDTO);
        PageDataResult pdr = new PageDataResult();
        PageHelper.startPage(page, limit);
        List<RestLogDTO> urList = infoMapper.getRestLog(searchDTO);
        // 获取分页查询后的数据
        PageInfo<RestLogDTO> pageInfo = new PageInfo<>(urList);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(urList);
        return pdr;
    }

    private void handleParam(RestLogSearchDTO searchDTO) {
        // 时间处理
        if (null != searchDTO) {
            if (StringUtils.isNotEmpty(searchDTO.getCreateDateStart())
                    && StringUtils.isEmpty(searchDTO.getCreateDateEnd())) {
                searchDTO.setCreateDateEnd(DateUtil.format(new Date()));
            } else if (StringUtils.isEmpty(searchDTO.getCreateDateStart())
                    && StringUtils.isNotEmpty(searchDTO.getCreateDateEnd())) {
                searchDTO.setCreateDateStart(DateUtil.format(new Date()));
            }
            if (StringUtils.isNotEmpty(searchDTO.getCreateDateStart())
                    && StringUtils.isNotEmpty(searchDTO.getCreateDateEnd())) {
                if (searchDTO.getCreateDateEnd().compareTo(searchDTO.getCreateDateStart()) < 0) {
                    String temp = searchDTO.getCreateDateStart();
                    searchDTO.setCreateDateStart(searchDTO.getCreateDateEnd());
                    searchDTO.setCreateDateEnd(temp);
                }
            }
        }
    }

    @Override
    public String delRestLog(Integer id) {
        mapper.deleteByPrimaryKey(id);
        return "ok";
    }

    @Override
    public String getRestLogWithBLOBs(Integer id, String logType) {
        RestLogWithBLOBs restLogWithBLOBs = mapper.selectByPrimaryKey(id);
        if (!Objects.isNull(restLogWithBLOBs)) {
            return "showReqData".equals(logType) ? restLogWithBLOBs.getReqData() : restLogWithBLOBs.getContent();
        }
        return null;
    }

    @Override
    public List<RestLogModel> exportRestLog(RestLogSearchDTO searchDTO) {
        handleParam(searchDTO);
        List<RestLogDTO> urList = infoMapper.getRestLog(searchDTO);
        if(!CollectionUtils.isEmpty(urList)){
            List<RestLogModel> models=Lists.newArrayList();
            for (RestLogDTO restLogDTO : urList) {
                RestLogModel restLogModel=new RestLogModel();
                BeanUtils.copyProperties(restLogDTO,restLogModel);
                models.add(restLogModel);
            }
            return models;
        }
        return Lists.newArrayList();
    }

    @Override
    public List<UserDeviceModel> exportUserDevice(RestLogSearchDTO searchDTO) {
        handleParam(searchDTO);
        return infoMapper.getUserDeviceInfos(searchDTO);
    }
}
